import json

from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from .models import *
from .serializers import DepartmentSerializers,DepartmentSubSerializers
from django.conf import settings

class DepartmentTypeView(APIView):
    def get(self,request):
        type=settings.DEPARTMENT_CLASSIFICATION.values()
        return Response(list(type))



class DepartmentView(ModelViewSet):
    queryset = Department.objects.all()
    serializer_class = DepartmentSerializers
    def get_queryset(self):
        if self.action=='list':
            return Department.objects.filter(pid=None)
        else:
            return Department.objects.all()
    def get_serializer_class(self):
        if self.action=='list':
            return DepartmentSerializers
        else:
            return DepartmentSubSerializers
    @action(methods=['get'], detail=False)
    def getCollege(self,request):
        datas=Department.objects.filter(pid=None).values()
        id=''
        for i in datas:
            if i['name']=='学院':
                id=i['id']
        queryset=Department.objects.filter(pid=id)
        ser = self.get_serializer(queryset, many=True)

        return Response(ser.data)
    def departmentTree(self,request):
        page = int(request.query_params.get('page', 1))
        page_size = int(request.query_params.get('page_size', 5))
        if page_size>20:
            page_size=20
        datas=Department.objects.filter(pid=None).values()
        result=[]
        for i in datas:
            dict_={}
            dict_['name']=i['name']
            dict_['id']=i['id']
            dict_['label']=i['name']
            dict_['value']=i['id']
            dict_['children']=[]
            for j in Department.objects.filter(pid=i['id']).values():
                childdict={}
                childdict['id']=j['id']
                childdict['name']=j['name']
                childdict['value']=j['id']
                childdict['label']=j['name']
                dict_['children'].append(childdict)
            result.append(dict_)
        start = (page - 1) * page_size
        data = {'count': len(result), 'data': list(result[start:page * page_size])}
        return Response(data)
    def addDepartment(self,request):
        data=json.loads(request.body.decode())
        if (data['pid']):
            result=Department.objects.create(name=data['name'],pid=Department.objects.get(id=data['pid']))
        else:
            result=Department.objects.create(name=data['name'])
        data=list(Department.objects.all().values())[0]
        return Response({"message":"添加数据成功","code":200,"data":data})
    def getALltwoDepart(self,request):
        data=[]
        for i in Department.objects.all().values():
            if i not in  Department.objects.filter(pid=None).values():
                data.append(i)
        return Response({"message":"添加数据成功","code":200,"data":data})
